<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	public function index()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('index');
		$this->load->model('auth');
		$resp=$this->auth->check();
		
		if($resp['state'])
		{
			$this->hotel();

		}

		else
		{
			$this->load->view('autorization');
		}
	}
	
	function hotel()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('hotel');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$params['list']=array(1=>'Hotel');
			$params['where']='admin';
			if($this->session->userdata('userType')==2)
			{
				$params['userlist']=$this->auth->userlist();
			}
			$this->load->view('head',$params);
			if($this->session->userdata('userType')>0)
			{
				$this->load->view('tourlist',$params);
			}
			else
			{
				$this->load->model('tourlist');
				$params['list']=$this->tourlist->getServicesUser(1);
				$this->load->view('usertourlist',$params);
			}
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}
	function excursion()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('excursion');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$params['list']=array(2=>'Excursion');
			$params['where']='admin';
			if($this->session->userdata('userType')==2)
			{
				$params['userlist']=$this->auth->userlist();
			}
			$this->load->view('head',$params);
		if($this->session->userdata('userType')>0)
			{
				$this->load->view('tourlist',$params);
			}
			else
			{$this->load->model('tourlist');
				$params['list']=$this->tourlist->getServicesUser(2);
				$this->load->view('usertourlist',$params);
			}
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}
	function tours()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('tours');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$params['list']=array(3=>'Tours');
			$params['where']='admin';
			if($this->session->userdata('userType')==2)
			{
				$params['userlist']=$this->auth->userlist();
			}
			$this->load->view('head',$params);
		if($this->session->userdata('userType')>0)
			{
				$this->load->view('tourlist',$params);
			}
			else
			{$this->load->model('tourlist');
				$params['list']=$this->tourlist->getServicesUser(3);
				$this->load->view('usertourlist',$params);
			}
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}	
	function city()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('city');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$params['list']=array(5=>'City');
			$params['where']='admin';
			if($this->session->userdata('userType')==2)
			{
				$params['userlist']=$this->auth->userlist();
			}
			$this->load->view('head',$params);
		if($this->session->userdata('userType')>0)
			{
				$this->load->view('tourlist',$params);
			}
			else
			{
				$this->load->model('tourlist');
				$params['list']=$this->tourlist->getServicesUser(5);
				$this->load->view('usertourlist',$params);
			}
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}
	function showplace()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('showplace');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$params['list']=array(4=>'Showplace');
			$params['where']='admin';
			if($this->session->userdata('userType')==2)
			{
				$params['userlist']=$this->auth->userlist();
			}
			$this->load->view('head',$params);
		if($this->session->userdata('userType')>0)
			{
				$this->load->view('tourlist',$params);
			}
			else
			{	
				$this->load->model('tourlist');
				$params['list']=$this->tourlist->getServicesUser(4);
				$this->load->view('usertourlist',$params);
			}
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}
	function getlist($type=0,$id=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('getlist');
		$type=(int)$type;
		if($type<=0)exit();
		$id=(int)$id;
		if($type<=0||$id<=0)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{	
			$this->load->model('options');
			header ("Content-Type:text/json");
			echo $this->options->getList($type,$id);
		}
		else
		{
			header ("Content-Type:text/json");
			echo "{state:'error',data:'not autorizated'}";
		}
	}
	function saveoptions()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		#$this->log('saveoptions');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$this->load->model('options');
			$type=(int)$this->input->post('type');
			$id=(int)$this->input->post('id');
			if($type!=0&&$id!=0)
			{
				$values=$this->input->post('option');
				#$this->log($values);
				$opt=(int)$this->input->post('opttype');
				#$this->log($opt);
				$this->options->saveoptions($type,$id,$values,$opt);
			}
		}
		else
		{	
			header ("Content-Type:text/json");
			echo "{status:'error',msg:'not autorizated'}";
		}
	}
	
	public function logout()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('logout');
		//
		
		$this->session->set_userdata('userType',false);
		$this->session->set_userdata('idUser',0);
		$this->session->set_userdata('is_auth',false);
		//$this->log("is_auth - ".$_SESSION['is_auth']);
		session_destroy();
		 //master
		$this->index();
	}
	public function testPost()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('testPost');
		$login = (isset($_POST['name'])) ? $_POST['name'] : "not set";
		$login2 = (isset($_GET['name'])) ? $_GET['name'] : "not set";
		$this->log($login." ".$login2);
		echo $login." ".$login2;
	}

	public function authfunc()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('authfunc');
		$login = (isset($_POST['login'])) ? $_POST['login'] : false; // get the requested page
		$password = (isset($_POST['pass'])) ? $_POST['pass'] : false; // get the requested page
		if($login&&$password)
		{
			$this->load->model('auth');
			$resp=$this->auth->check($login,$password);
			if(!$resp['state'])
			{
				echo $resp['message'];
				$this->load->view('autorization');
			}
			else
			{
				$this->index();
			}
		}
		else
		{
				$this->load->view('autorization');
		}
	}
	public function do_register()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('do_register');
		$this->load->model('auth');
		$params['userlist']="";
		if($this->session->userdata('userType')==2)
		{
			$params['userlist']=$this->auth->userlist();
		}
		$login = (isset($_POST['login'])) ? $_POST['login'] : exit(); // get the requested page
		$pass = (isset($_POST['pass'])) ? $_POST['pass'] : exit(); // get the requested page
		$email = (isset($_POST['mail'])) ? $_POST['mail'] : ""; // get the requested page
		$site = (isset($_POST['site'])) ? $_POST['site'] : ""; // get the requested page
		$country = (isset($_POST['country'])) ? $_POST['country'] : ""; // get the requested page
		if($login==""||$pass=="")
		{
			
			$this->load->view('head',$params);
			echo "Поля логин и пароль не могут быть пустыми";
			$this->load->view('registration');
			$this->load->view('end');

		}
		else
		{
			
			$resp=$this->auth->register($login,$pass,$email,$site,$country);
			if($resp['state'])
			{
				$this->load->view('head',$params);
				echo "Регистрация пользователя прошла успешно";
				$this->load->view('registration');
				$this->load->view('end');
			}
			else
			{
				$this->load->view('head',$params);
				echo $resp['message'];
				$this->load->view('registration');
				$this->load->view('end');
			}
		}

	}
	public function foto()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('foto');
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state']&&$this->session->userdata('userType')>0)
		{
			$params['userlist']="";
			$params['where']='mfoto';
			$params['userlist']=$this->auth->userlist();
				
			$this->load->view('head',$params);
			$this->load->view('loadfoto');
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	
	}
	public function register()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('register');
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state']&&$this->session->userdata('userType')==2)
		{
			$params['userlist']="";
			$params['where']='register';
				$params['userlist']=$this->auth->userlist();
			
			$this->load->view('head',$params);
			$this->load->view('registration');
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}

	}
	public function show($type="",$idUser=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('show');
		if($type=="")
		{
			
			exit();
		}
		$type=(int)$type;
		$this->load->model('auth');
		$resp=$this->auth->check();
		
		$idUser=(int)$idUser;
		 if($resp['state'])
		{
			$page = (isset($_POST['page'])) ? (int)$_POST['page'] : 1; // get the requested page
			$limit = (isset($_POST['rows'])) ? (int)$_POST['rows'] : 10; // get how many rows we want to have into the grid
			$sidx = (isset($_POST['sidx'])) ? $_POST['sidx'] : 1; // get index row - i.e. user click to sort
			$sord = (isset($_POST['sord'])) ? $_POST['sord'] : 'asc'; // get the direction
			if($sord != 'asc' && $sord != 'desc') $sord = 'asc';

			$aFields = array('id', 'tourTitle', 'days', 'nights');
			if(!in_array($sidx, $aFields))$sidx = 'id';

			if(!$sidx) $sidx =1;

			$this->load->model('tourlist');
			$response=$this->tourlist->show($page,$limit,$sidx,$sord,$type,$idUser);
			echo $response;
		}
	}
	public function edittour($type="")
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('edittour');
		if(!isset($_POST['oper']))exit;
		if($type=="")
		{
			exit();
		}
		$type=(int)$type;
		if($type==0)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')!=0)
		{
			$show = (isset($_POST['show'])) ? mysql_escape_string(htmlspecialchars($_POST['show'])) : "";
			$name = (isset($_POST['name'])) ? mysql_escape_string(htmlspecialchars($_POST['name'])) : "";
			$desc = (isset($_POST['desc'])) ? mysql_escape_string(htmlspecialchars($_POST['desc'])) : '';
			$show=$show=='Yes'?1:0;
			$aOppers = array('add','edit','del');
			if(!in_array($_POST['oper'], $aOppers))exit;
			$oper = $_POST['oper'];
			
			if($oper == 'del' || $oper == 'edit')
			{
			    $Id = (int)$_POST['id'];
			    if($Id == 0)exit;
			}
		
			$this->load->model('tourlist');
			switch($oper)
			{
			    case 'add': $this->tourlist->addtour($name,$desc,$type);break;
			    case 'edit': $this->tourlist->edit($Id,$name, $desc,$show);break;
			    case 'del': $this->tourlist->deleteTour($Id);break;
			}
		}

	}
	function cloneservice()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('cloneservice');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')>0)
		{
			$id=(int)$this->input->post('cloneId');
			if($id==0)die('Hacking attempt');
			$this->load->model('tourlist');
			$this->tourlist->cloneservice($id);
		}
	}
	public function showSubGrid($id="")
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('showSubGrid');
		if($id=="")exit();
		$id=(int)$id;
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$this->load->model('options');
			$responce=$this->options->show($id);
			//$this->log($responce);
			echo $responce;
		}

	}
	public function editSubGrid($ServId="")
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('editSubGrid');
		if(!isset($_POST['oper']))exit;
		if($ServId=="")exit();
		$ServId=(int)$ServId;
		$this->log('editSubGrid');

		$name = (isset($_POST['name'])) ? mysql_escape_string(htmlspecialchars($_POST['name'])) : "";
		$desc = (isset($_POST['desc'])) ? mysql_escape_string(htmlspecialchars($_POST['desc'])) : '';

		$aOppers = array('add','edit','del');
		if(!in_array($_POST['oper'], $aOppers))exit;
		$this->log(1);
		$oper = $_POST['oper'];
		if($oper == 'del' || $oper == 'edit')
		{
			$this->log("POST_ID".$_POST['id']);
			$Id = $_POST['id'];
			if($Id =="")exit;
		}
		$this->log($ServId."-".$Id."-".$desc);
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state']&&$this->session->userdata('userType')!=0)
		{
			$this->load->model('options');
			switch ($oper)
			{
				case 'del':$this->options->del($ServId,$Id);break;
				case 'add':$this->options->add($ServId,$name);break;
				case 'edit':$this->options->edit($ServId,$Id,$desc);break;
			}
			$responce=$this->options->show($id);
		}
	}

	public function price($type="",$id="")
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('price');
		if($type==""||$id=="")exit();
		$type=(int)$type;
		$id=(int)$id;
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			//$this->log('Show price');
			$this->load->model('pricelist');
			$temp=$this->pricelist->getList($type,$id);
			$params['list']=$temp['list'];
			$params['type']=$temp['type'];
			$params['name']=$temp['name'];
			$params['where']='admin';
			//print_r($temp);
			//$params['name']='als;d';
			$params['rows']=$this->pricelist->getRowListToCalendar($id);
			$params['service']=$id;
			//print_r($params);
			$this->load->view('head',$params);
			$this->load->view('pricelist',$params);
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}
	function showAllTables($type=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('showAllTables');
		$type=(int)$type;
		if($type==0)
		{
			die();
		}
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$this->load->model('pricelist');
			echo $this->pricelist->showAllTables($type);
		}
		
	}
	function getlisttableoptions($type=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('getlisttableoptions');
		$type=(int)$type;
		if($type==0)exit();
		$this->load->model('tableoptions');
		$responce=$this->tableoptions->getlistoptions($type);
		header ("Content-Type:text/json");
		echo $responce;
	}
	public function showPrice($year="",$month="",$idServ="")
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log(' showPrice');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			if($year==""||$month==""||$idServ=="")exit();
			$this->load->model('pricelist');
			$responce=$this->pricelist->getPrice($year,$month,$idServ);
			
			echo $responce;
		}
	}

	public function showEkskurs()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('showEkskurs');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$page = (isset($_POST['page'])) ? (int)$_POST['page'] : 1; // get the requested page
			$limit = (isset($_POST['rows'])) ? (int)$_POST['rows'] : 10; // get how many rows we want to have into the grid
			$sidx = (isset($_POST['sidx'])) ? $_POST['sidx'] : 1; // get index row - i.e. user click to sort
			$sord = (isset($_POST['sord'])) ? $_POST['sord'] : 'asc'; // get the direction
			if($sord != 'asc' && $sord != 'desc') $sord = 'asc';

			$aFields = array('id', 'name', 'desc');
			if(!in_array($sidx, $aFields))$sidx = 'id';

			if(!$sidx) $sidx =1;


			$this->load->model('ekskurs');
			$response=$this->ekskurs->show($page,$limit,$sidx,$sord);
			echo $response;
		}
	}
	public function editEkskurs()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('editEkskurs');
		if(!isset($_POST['oper']))exit;
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$name = (isset($_POST['name'])) ?  mysql_escape_string(htmlspecialchars($_POST['name'])) : exit();
			$desc = (isset($_POST['desc'])) ?  mysql_escape_string(htmlspecialchars($_POST['desc'])) : "";

			$aOppers = array('add','edit','del');
			if(!in_array($_POST['oper'], $aOppers))exit;
			$oper = $_POST['oper'];

			if($oper == 'del' || $oper == 'edit')
			{
				$Id = (int)$_POST['id'];
				if($Id == 0)exit;
			}
			$this->load->model('ekskurs');
			switch($oper)
			{
				case 'add': $this->ekskurs->addtour($name,$desc);break;
				case 'edit': $this->ekskurs->edit($Id, $name, $desc);break;
				case 'del': $this->ekskurs->del($Id);break;
			}
		}

	}
	/*public function tourdates($id=0,$yy="",$mm="")
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		//$id,$yy,$mm
		$this->log('tourdates');
		$this->load->model('auth');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$tourId = $id ? (int)$id : 0;
			$month = $mm ? (int)$mm : date('m');
			$year = $yy ? (int)$yy :date('Y');
			//$tourId = (isset($_GET['tourid'])) ? (int)$_GET['tourid'] : 0;
			$params['userlist']="";
			if($this->session->userdata('userType')==2)
			{
				$params['userlist']=$this->auth->userlist();
			}
			$params['where']='admin';
			$this->load->view('head',$params);
			$this->load->model('tourdates');

			$params['tourTitle']=$this->tourdates->tourTitle($tourId);
			$params['list_tours']=$this->tourdates->listTours($tourId);
			$params['month']=$month;
			$params['year']=$year;
			$params['tourId']=$tourId;
			//print_r($params);
			$this->load->view('tourdates',$params);

			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}
	public function datatour($y="",$m="",$tourId="",$type="")
	{
		$this->log('datatour');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$tourId = $tourId ? (int)$tourId : 0;
			$type = $type ? (int)$type : 3;
			$m = $m ? (int)$m : 1;
			$y = $y ? (int)$y : date('Y');

			$this->load->model('tourdates');
			echo $this->tourdates->json($y,$m,$tourId,$type);
		}
	}
	public function deldatatour($year=0,$month=0,$tourId=0,$table=0)
	{
		$this->log('deldatatour');
		$this->load->model('auth');
		$resp=$this->auth->check();
	 	if($resp['state'])
		{
		//	if(!isset($_POST['oper'])){$this->Log('oper false');exit;}
			if($year==0||$month==0||$tourId==0||$table==0){exit;}
			//$type = (isset($_POST['id'])) ? (int)$_POST['id'] : exit;
			$this->load->model('tourdates');
	    	$this->tourdates->delete($year,$month,$tourId,$table);
		}

	}*/
	public function editprice($year=0,$month=0,$idtable=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('editprice');
		if(!isset($_POST['oper'])){$this->Log('oper false');exit;}
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			if($year==0||$month==0||$idtable==0){$this->Log('$year==0||$month==0||$table==0 false');exit;}
			$type = (isset($_POST['id'])) ? (int)$_POST['id'] : exit;
			$days=array();
			$m=$month;
			$y=$year;
			$n=($m!=2?(($m%2)^($m>7))+30:(!($y%400)||!($y%4)&&($y%25)?29:28));

			for($d=1;$d<=$n;$d++)
			{
				$days['d'.$d] = '';
			}

			foreach($days as $key => $val)
			{
	 			 if(!isset($_POST[$key]))continue;
				  $days[$key] = (int)$_POST[$key];
				 
			}

			$aOpers = array('edit','del');


			if(!in_array($_POST['oper'], $aOpers))exit;
			$oper = $_POST['oper'];

			switch($oper)
			{
	    		case 'edit':
					//editTourDate($tourDateStart, $tourDateEnd, $aPrices, $aWeekdays, $tourDateId, $tourId);
					$this->load->model('pricelist');
					
					$this->pricelist->edit($idtable,$type,$days,$year,$month);
				break;
	    		case 'del':
	    			$this->load->model('tourdates');
	    			$this->tourdates->clear($line,$year,$month,$tourId,$table);
					//delTourDate($tourDateId);
				break;
			}
		}
	}
	public function addMonthPrice()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('addMonthPrice');
		$this->load->model('auth');
		$resp=$this->auth->check();
	 	if($resp['state'])
		{
			$year=isset($_POST['year'])?(int)$_POST['year']:exit;
			$month=isset($_POST['month'])?(int)$_POST['month']:exit;
			$tourid=isset($_POST['tourid'])?(int)$_POST['tourid']:exit;
			$type=isset($_POST['type'])?(int)$_POST['type']:exit;
			$DBL=isset($_POST['DBL'])?(int)$_POST['DBL']:0;
			$TRPL=isset($_POST['TRPL'])?(int)$_POST['TRPL']:0;
			$SNGL=isset($_POST['SNGL'])?(int)$_POST['SNGL']:0;
			$CHLD=isset($_POST['CHLD'])?(int)$_POST['CHLD']:0;
			$HB=isset($_POST['HB'])?(int)$_POST['HB']:0;

			$d[1]=isset($_POST['pn'])?$_POST['pn']:false;
			$d[2]=isset($_POST['vt'])?$_POST['vt']:false;
			$d[3]=isset($_POST['sr'])?$_POST['sr']:false;
			$d[4]=isset($_POST['ch'])?$_POST['ch']:false;
			$d[5]=isset($_POST['pt'])?$_POST['pt']:false;
			$d[6]=isset($_POST['sb'])?$_POST['sb']:false;
			$d[0]=isset($_POST['vs'])?$_POST['vs']:false;
			$this->load->model('tourdates');
			$this->tourdates->addAllMonth($year,$month,$tourid,$DBL,$TRPL,$SNGL,$CHLD,$HB,$d,$type);
		}
	}
	public function load($id=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('load');
 		$this->load->model('fotos');
  		$this->fotos->savefoto($id);
  	
	}
	function getfolders()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('getfolders');
		$this->load->model('fotos');
		header ("Content-Type:text/json");
		echo $this->fotos->getfolders();
	}
	function getfotos($id=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('getfotos');
		if($id==0)die();
		$this->load->model('fotos');
		header ("Content-Type:text/json");
		echo $this->fotos->getfotos($id);
	}
	function editfotos($id=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$id=(int)$id;
		if($id==0)exit();
		$name = (isset($_POST['name'])) ? mysql_escape_string(htmlspecialchars($_POST['name'])) : "";
		$this->load->model('fotos');
		$this->fotos->editfotos($id,$name);
	}
	function deletefotos($id=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		if($id==0)exit();
		$this->load->model('fotos');
		header ("Content-Type:text/json");
		echo $this->fotos->deletefotos($id);
	}
	function clients($userType=5){
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('clients');
		$userType=(int)$userType;
		if($userType<0||$userType>2)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$params['userlist']="";
			
			$params['userlist']=$this->auth->userlist();
			$params['where']='userlist';
			$this->load->view('head',$params);
			switch($userType)
			{
				case 0:
					$this->load->view('userclientlist');
				break;
				case 1:
					$this->load->view('agentclientlist');
				break;
				case 2:
					$this->load->view('clientlist');
				break;
			}
			
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}
	function changeUserId($id=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('changeUserId');
		if($id==0)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')==2)
		{
			$this->session->set_userdata('idUser',$id);
		}
	}
	function getclientlist()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		//$this->log("ad ".$ad);
		$this->log('getclientlist');
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state']&&$this->session->userdata('userType')==2)
		{
			$this->load->model('clients');
			$result = $this->clients->get_list();
			echo $result;
		}
	}
	function getagentclientlist()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('getagentclientlist');
		//$this->log("ad ".$ad);
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')==1)
		{
			$this->load->model('clients');
			$result = $this->clients->getUserList();
			echo $result;
		}
	}
	function getuserclientlist()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		//$this->log("ad ".$ad);
		$this->log('getuserclientlist');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')==0)
		{
			$this->load->model('clients');
			$result = $this->clients->getAgentList();
			echo $result;
		}
	}
	function getOtpr($idPrin=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('getOtpr');
		$idPrin=(int)$idPrin;
		if($idPrin==0)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')==2)
		{
			$this->load->model('clients');
			echo $this->clients->getOtpr($idPrin);
		}
	}
	function setOtpr()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log(' setOtpr');
		if(!isset($_POST['oper']))exit;
		
		
		
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')==2)
		{
			$check = (isset($_POST['checked'])) ? mysql_escape_string(htmlspecialchars($_POST['checked'])) : exit();
			$idPrin = (isset($_POST['idPrin'])) ?(int)($_POST['idPrin']) : 0;
			
			if($idPrin==0)exit();
			$aOppers = array('edit');
			if(!in_array($_POST['oper'], $aOppers))exit();
			$oper = $_POST['oper'];
				
			if( $oper == 'edit')
			{
				$Id = (int)$_POST['id'];
				$this->log($check." v ".$idPrin." v ".$Id);
				if($Id == 0)exit;
			}
			$this->load->model('clients');
			$this->clients->setOtpr($Id,$idPrin,$check);
		}
	}
	
	function addType()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log(' addType');
		$this->load->model('auth');
		$resp=$this->auth->check();
		$name = (isset($_POST['name'])) ?  mysql_escape_string(trim(htmlspecialchars($_POST['name']))) : exit();

		 if($resp['state']&&$this->session->userdata('userType')==2&&$name!="")
		{
			$this->load->model('tourlist');
			echo $this->tourlist->addType($name);

		}
	}
	function getTypeList()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('getTypeList');
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state']&&$this->session->userdata('userType')!=0)
		{
			$this->load->model('tourlist');
			echo $this->tourlist->getTypeList();
		}
		else
		{
			echo json_encode(array());
		}
	}
	function addService()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('addService');
		$id = (isset($_POST['addService'])) ?  (int)($_POST['addService']) : 0;
		if($id==0)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')!=0)
		{
			$this->load->model('tourlist');
			echo $this->tourlist->addService($id);
		}
	}
	function getRowList()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('getRowList');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')!=0)
		{
			$this->load->model('pricelist');
			echo $this->pricelist->getRowList();
		}
	}
	function addTableInService($idServ=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('addTableInService');
		$idServ=(int)$idServ;
		if($idServ==0)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')!=0)
		{
			$name = (isset($_POST['name'])) ?  mysql_escape_string(trim(htmlspecialchars($_POST['name']))) : exit();
			$desc=	(isset($_POST['desc'])) ?  mysql_escape_string(trim(htmlspecialchars($_POST['desc']))) : "";
			$num=(isset($_POST['numRows'])) ?  (int)($_POST['numRows']) : 0;
			$rows=array();
			$rows=($_POST['n']);
			/*for($i=1;$i<=$num;$i++)
			{
				$rows[]=(isset($_POST['n'.$i])) ?  (int)($_POST['n'.$i]) : 0;
			}*/
			//print_r($rows);
			$this->load->model('pricelist');
			echo $this->pricelist->addTableInService($idServ,$name,$desc,$rows['price']);
		}
	}
	function addPrices()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('addPrices');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')!=0)
		{
			$table = (isset($_POST['idTable'])) ?  (int)($_POST['idTable']) : exit();
			$dates=(isset($_POST['dates'])) ?  mysql_escape_string(trim(htmlspecialchars($_POST['dates']))) : "";
			$skidka = (isset($_POST['Skidka'])) ?  (int)($_POST['Skidka']) :exit();
			//if($skidka>=100||$skidka<0)$skidka=0;
			$skidka=$skidka==1?1:0;
			$rows=(isset($_POST['r']))?$_POST['r']:array();
			$cRows=array();
			foreach($rows as $key=>$val)
			{
				if($val=='')
				{
					$cRows[(int)$key]=-1;
				}
				else
				{
					$cRows[(int)$key]=(int)$val;
				}
			}
			//this->log($cRows);
			$this->load->model('pricelist');
			$this->pricelist->setprice($table,$dates,$cRows,$skidka);
			//print_r($rows);
		}
	}
function cloneTableInService($idServ=0)
{
	header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');

	$this->log('cloneTableInService');
	$idServ=(int)$idServ;
	if($idServ==0)exit();
	$this->load->model('auth');
	$resp=$this->auth->check();
	 if($resp['state']&&$this->session->userdata('userType')!=0)
	{

		$idTable = (isset($_POST['idTable'])) ?  (int)($_POST['idTable']) : exit();
		$name = (isset($_POST['name'])) ?  mysql_escape_string(trim(htmlspecialchars($_POST['name']))) : exit();
		$desc=	(isset($_POST['desc'])) ?  mysql_escape_string(trim(htmlspecialchars($_POST['desc']))) : "";
		$this->Log("Принимаем данные для клонирования--".$name."---".$desc."---".$idServ);
		$this->load->model('pricelist');
		echo $this->pricelist->cloneTableInService($idServ, $name, $desc, $idTable);
	}
}
function deleteTableData($idServ=0)
{
	header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
	
	$this->log('deleteTableData');
	$idServ=(int)$idServ;
	if($idServ==0)exit();
	$this->load->model('auth');
	$resp=$this->auth->check();
	 if($resp['state']&&$this->session->userdata('userType')!=0)
	{

		$idTable = (isset($_POST['idTable'])) ?  (int)($_POST['idTable']) : exit();
		$this->load->model('pricelist');
		$this->pricelist->deleteTableData($idTable);
	}
}
function deleteTable($idServ=0)
{
	header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
	
	$this->log('deleteTable');
	$idServ=(int)$idServ;
	if($idServ==0)exit();
	$this->load->model('auth');
	$resp=$this->auth->check();
	 if($resp['state']&&$this->session->userdata('userType')!=0)
	{
		$idTable = (isset($_POST['idTable'])) ?  (int)($_POST['idTable']) : exit();
		$this->load->model('pricelist');
		$this->pricelist->deleteTable($idTable);
	}
}
function deleteTables($idServ=0)
{
	header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
	
	$this->log('deleteTables');
	$idServ=(int)$idServ;
	if($idServ==0)exit();
	$this->load->model('auth');
	$resp=$this->auth->check();
	if($resp['state']&&$this->session->userdata('userType')!=0){
		$this->load->model('pricelist');
		$this->pricelist->deleteTables($idServ);
	}
}
function deleteTableDataByMonth($idServ=0, $month, $year){
	header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
	
	$this->log('deleteTableDataByMonth');
	$idServ=(int)$idServ;
	if($idServ==0)exit();
	$this->load->model('auth');
	$resp=$this->auth->check();
	 if($resp['state']&&$this->session->userdata('userType')!=0)
	{

		$idTable = (isset($_POST['idTable'])) ?  (int)($_POST['idTable']) : exit();
		$date = $year.'-'.$month;
		$this->load->model('pricelist');
		$this->pricelist->deleteTableDataByMonth($idTable, $date);
	}
}
	function showUserPrice($y=0,$m=0,$idServ=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('showUserPrice');
		$y=(int)$y;
		$m=(int)$m;
		$idServ=(int)$idServ;
		if($y==0||$m==0||$idServ==0)exit();
		$this->load->model('agentprice');
		echo $this->agentprice->show($y,$m,$idServ);
	}
	function editUserPrice($y=0,$m=0,$idServ=0)
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('editUserPrice');
		$y=(int)$y;
		$m=(int)$m;
		$idServ=(int)$idServ;
		if($y==0||$m==0||$idServ==0)exit();
		$type = (isset($_POST['id'])) ? (int)$_POST['id'] : exit;
		$days=array();
		
		$n=($m!=2?(($m%2)^($m>7))+30:(!($y%400)||!($y%4)&&($y%25)?29:28));
		
		for($d=1;$d<=$n;$d++)
		{
			$days['d'.$d] = '';
		}
		
		foreach($days as $key => $val)
		{
			if(!isset($_POST[$key]))continue;
			$days[$key] = (int)$_POST[$key];
		}
		$aOpers = array('edit');
		if(!in_array($_POST['oper'], $aOpers))exit;
		$oper = $_POST['oper'];
		switch($oper)
		{
			case 'edit':
				$this->load->model('agentprice');
				$this->agentprice->edit($idServ,$type,$days,$y,$m);
			break;
		}
	}
	function addUserPrices()
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log(' addUserPrices');
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')==0)
		{
			$table = (isset($_POST['idServ'])) ?  (int)($_POST['idServ']) : exit();
			$dates=(isset($_POST['dates'])) ?  mysql_escape_string(trim(htmlspecialchars($_POST['dates']))) : "";
			$skidka = (isset($_POST['Skidka'])) ?  (int)($_POST['Skidka']) :exit();
			//if($skidka>=100||$skidka<0)$skidka=0;
			$skidka=$skidka==1?1:0;
			$rows=(isset($_POST['r']))?$_POST['r']:array();
			$cRows=array();
			foreach($rows as $key=>$val)
			{
				$cRows[(int)$key]=(int)$val;
			}
			$this->load->model('agentprice');
			$this->agentprice->setprice($table,$dates,$cRows,$skidka);
			//print_r($rows);
		}
	}
	function deleteUserTableDataByMonth($idServ=0, $month, $year){
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('deleteUserTableDataByMonth');
		$idServ=(int)$idServ;
		if($idServ==0)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')==0)
		{

			$date = $year.'-'.$month;
			$this->load->model('agentprice');
			$this->agentprice->deleteTableDataByMonth($idServ, $date);
		}
	}
	function deleteUserTableData($idServ=0){
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$idServ=(int)$idServ;
		if($idServ==0)exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state']&&$this->session->userdata('userType')==0)
		{
			$this->load->model('agentprice');
			$this->agentprice->deleteTableData($idServ);
		}
	}
	function orders($type='')
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->log('orders');
		if($type=='')exit();
		if($type!=$this->session->userdata('userType'))exit();
		$this->load->model('auth');
		$resp=$this->auth->check();
		 if($resp['state'])
		{
			$params['where']='orders';
			$this->load->view('head',$params);
			$this->load->model('orders');
			$param['list']=$this->orders->get_orders_list($this->session->userdata('idUser'));
			//exit (print_r($param['list']));
			$this->load->view('orders',$param);
			$this->load->view('end');
		}
		else
		{
			$this->load->view('autorization');
		}
	}
	function tmp_orders_form()
	{
			$type = 2;
			$params['where']='orders';
			$this->load->view('head',$params);
			$this->load->model('orders');
			$param['list']=$this->orders->getlist($type,$this->session->userdata('idUser'));
			$this->load->view('order_form',$param);
			$this->load->view('end');
	}
	function date()
	{
		echo date('Y m d H:m:s');
	}
	function changelang($lang="russian")
	{
		header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
		
		$this->session->set_userdata('lang',$lang);
	 	//
	 	header('Refresh: 1; URL=/admin/');
	 	echo "<center>Change language to ".$lang." </center>";
	}
	function change_description()
	{
		$this->load->model('auth');
		$resp=$this->auth->check();
		$id=(int)$this->input->post('id');
		$desc=mysql_escape_string(trim(htmlspecialchars($this->input->post('description'))));
		
		if($resp['state'])
		{
			$this->load->model('pricelist');
			$this->pricelist->update_table_descr($id,$desc);
		}
	}
	function change_name()
	{
		$this->load->model('auth');
		$resp=$this->auth->check();
		$id=(int)$this->input->post('id');
		$desc=mysql_escape_string(trim(htmlspecialchars($this->input->post('description'))));
		
		if($resp['state'])
		{
			$this->load->model('pricelist');
			$this->pricelist->update_table_name($id,$desc);
		}
	}
	function editstruct()
	{
		$this->load->model('auth');
		$resp=$this->auth->check();
		//$this->log($_POST);
		$id=(int)$this->input->post('idTable');
		$struct=$this->input->post('struct');
		if(!is_array($struct))
		{
			exit();
		}
		if($resp['state'])
		{
			$this->load->model('pricelist');
			$this->pricelist->update_struct($id,$struct);
		}
	}
	function hotel_2plus2()
	{
		
		$this->load->model('auth');
		$resp=$this->auth->check();
		$id=(int)$this->input->post('id');
		$dblchld=$this->input->post('dblchld')=='true'?1:0;
		$age=(int)$this->input->post('age');
		
		if($resp['state'])
		{
			$this->load->model('pricelist');
			$this->pricelist->hotel22($id,$age,$dblchld);
		}
	}
	function get_hotel22()
	{
		header("Content-Type: application/json");
		$this->load->model('auth');
		$resp=$this->auth->check();
		$id=$_POST['id'];
		if($resp['state'])
		{
			$this->load->model('pricelist');
			echo $this->pricelist->get_hotel22($id);
		}
	}
	function save_setting()
	{
		$id = (int) $this->input->post("id");
		$url = $this->input->post("url");
		$width = (int) $this->input->post("width");
		$height = (int) $this->input->post("height");
		if( $id > 0 ){
			$this->load->model("clients");
			$this->clients->save_setting($id, $url, $width, $height);
		}
	}
	function addpriceinalltables($id=0)
	{
		$id=(int)$id;
		if($id==0)
		{
			exit();
		}
		$this->load->model('auth');
		$resp=$this->auth->check();
		$date=$this->input->post('dates');
		$prices=$this->input->post('price');
		
		if($resp['state'])
		{
			$this->load->model('pricelist');
			$this->pricelist->addpriceinalltables($id,$date,$prices);
		}
	}
	function get_periods($id=0)
	{
		header("Content-Type: application/json");
		$id=(int)$id;
		if($id==0)
		{
			exit();
		}
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state'])
		{
			$this->load->model('pricelist');
			echo $this->pricelist->get_periods($id);
		}
	}
	function delete_period($id=0)
	{
		$name=$this->input->post('name');
		$id=(int)$id;
		if($id==0)
		{
			exit();
		}
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state'])
		{
			$this->load->model('pricelist');
			$this->pricelist->delete_period($name,$id);
		}
	}
	function add_period()
	{
		$id=(int)$this->input->post('id');
		if($id==0)
		{
			exit();
		}
		$name=$this->input->post('name');
		$short=$this->input->post('short');
		$periods=$this->input->post('periods');
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state'])
		{
			$this->load->model('pricelist');
			$this->pricelist->add_period($id,$name,$short,$periods);
		}
	}
	function php()
	{
		$this->load->model('auth');
		$resp=$this->auth->check();
		if($resp['state'])
		{
			phpinfo();
		}
	}
}

/* End of file welcome.php */
